package someTestExcemple.huaweiOd.logicAnalysis;

//华为OD机试 - 员工派遣 - （Java 2024 D卷 200分）
//https://blog.csdn.net/guorui_java/article/details/139700550
import java.util.*;
public class StaffDispatch {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        int y = sc.nextInt();
        int cntx = sc.nextInt();
        int cnty = sc.nextInt();
        sc.close();
        int k = findMinK(x,y,cntx,cnty);
        System.out.println(k);
    }

    private static int findMinK(int x, int y, int cntx, int cnty) {
        int canGoX = 0;
        int canGoY = 0;
        int canGoBoth = 0;
        int k = 0;
        while (true) {
            k++; //从1开始
            if (k % x != 0 && k % y != 0) {
                canGoBoth++;
            } else if (k % x != 0) {
                canGoX++;
            } else if (k % y != 0) {
                canGoY++;
            }
            // 先分配只能去一个国家的员工
            int remainX = Math.max(0, cntx - canGoX);
            int remainY = Math.max(0, cnty - canGoY);
            //再分配两个都可以去的国家
            if (canGoBoth >= remainX + remainY) {
                break;
            }
        }
        return k;
    }
}
